/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.core.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.mortbay.thread.QueuedThreadPool;

import com.cqcis.uip.base.framework.core.ServletContainer;

/**
 * ServletContainer服务器 工厂
 * 
 * @author huangmy
 * @date   2009-3-25
 */
public final class ServletContainerServerFactory {
	private static ServletContainerServerFactory instance = new ServletContainerServerFactory(); // FACTORY
	private Map<String, ServletContainerServer> servletServers = new HashMap<String, ServletContainerServer>(); // 所有的Server
	private int threadpoolMaxThreads = 8; // 线程池-最大线程数
	private int threadpoolMinThreads = 5; // 线程池-最小线程数
	private boolean showServiceDetails = true;
	
	public ServletContainerServerFactory() {	
		if (instance != null) {
			this.servletServers.putAll(instance.servletServers); // 将server移到新的工厂中
			instance.servletServers.clear();
		}
		instance = this;
	}
	
	/**
	 * 单例
	 * 
	 * @return
	 */
	public static ServletContainerServerFactory getInstance() {
		return instance;
	}
	
	/**
	 * 获取Server
	 * 
	 * @param port
	 * @return
	 * @throws Exception 
	 */
    public synchronized ServletContainer getServer(int port) throws Exception {
    	String portKey = String.valueOf(port);
    	ServletContainerServer s = servletServers.get(portKey);
    	if (s == null || s.isStopped() || s.isStopping()) {
    		s = new ServletContainerServer(port, showServiceDetails);
    		if (!s.isStarted() && !s.isStarting()) {
    			QueuedThreadPool qtp = new QueuedThreadPool();
    			qtp.setMaxThreads(this.threadpoolMaxThreads);
    			qtp.setMinThreads(this.threadpoolMinThreads);
    			qtp.setName("ServletContainer");
    			s.setThreadPool(qtp);
    			
        		s.start(); // START
        	}
    		servletServers.put(portKey, s);
    	}
    	
    	return s;
    }
    
    /**
     * 销毁指定端口上的Server
     * 
     * @param port
     */
    public synchronized void destory(int port) {
    	String portKey = String.valueOf(port);
    	ServletContainerServer s = servletServers.remove(portKey);
    	if (s != null) {
    		try {
				if (!s.isStopped() && !s.isStopping()) {
					s.stop();
				}					
			} catch (Exception e) {
			}
    	}
    }
    
    /**
     * 销毁所有的Server
     * 
     */
    public synchronized void destoryAll() {
    	for (Entry<String, ServletContainerServer> entry : servletServers.entrySet()) {
    		ServletContainerServer s = entry.getValue();
    		if (s != null) {
    			try {
    				if (!s.isStopped() && !s.isStopping()) {
    					s.stop();
    				}					
				} catch (Exception e) {
				}
    		}
    	}
    	
    	servletServers.clear();
    }

	public void setThreadpoolMaxThreads(int threadpoolMaxThreads) {
		this.threadpoolMaxThreads = threadpoolMaxThreads;
	}

	public void setThreadpoolMinThreads(int threadpoolMinThreads) {
		this.threadpoolMinThreads = threadpoolMinThreads;
	}

	public void setShowServiceDetails(boolean showServiceDetails) {
		this.showServiceDetails = showServiceDetails;
	}
}
